package com.sqx.modules.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.Result;
import com.sqx.modules.course.dao.AdsDao;
import com.sqx.modules.course.entity.Ads;
import com.sqx.modules.course.entity.Course;
import com.sqx.modules.course.service.AdsService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class AdsServiceImpl  extends ServiceImpl<AdsDao, Ads> implements AdsService {
    @Autowired
    private AdsDao adsDao;

    /**
     * 添加广告
     * @param ads
     */
    @Override
    public void addAds(Ads ads) {
        ads.setCreateTime(new Date());
        adsDao.insert(ads);
    }

    @Override
    public void updateAds(Ads ads) {
        ads.setUpdateTime(new Date());
        adsDao.updateById(ads);
    }

    @Override
    public Result selectAdsList(Integer page, Integer limit, Integer adsId, String adsName) {
        LambdaUpdateWrapper<Ads> adsLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        adsLambdaUpdateWrapper.eq(ObjectUtils.isNotEmpty(adsId), Ads::getAdsId, adsId);
        adsLambdaUpdateWrapper.eq(Ads::getDelStatus,1);
        adsLambdaUpdateWrapper.like(ObjectUtils.isNotEmpty(adsName), Ads::getAdsName, adsName);
        Page<Ads> pages=new Page<>(ObjectUtils.isEmpty(page)?1:page,ObjectUtils.isEmpty(limit)?5:limit);
        IPage<Ads> adsIPage = adsDao.selectPage(pages, adsLambdaUpdateWrapper);
        return Result.success().put("data",adsIPage);
    }

    @Override
    public Result selectAdsById(Integer adsId) {
        Ads ads = adsDao.selectById(adsId);
        return Result.success().put("data",ads);
    }

    @Override
    public Result deleteAds(Integer[] adsIds) {
        LambdaUpdateWrapper<Ads> adsLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        adsLambdaUpdateWrapper.in(Ads::getAdsId, Arrays.asList(adsIds));
        adsLambdaUpdateWrapper.set( Ads::getDelStatus,2);
        adsLambdaUpdateWrapper.set(Ads::getDeleteTime,new Date());
        adsDao.update(null,adsLambdaUpdateWrapper);
        return Result.success();
    }
}
